﻿using libcontrol;

namespace libemscontrol.act
{
    class DeviceMsg
    {


    }
    class RecvMemData
    {
        public ControlMsgCallback m_delControlBoardMsg = null;

        public static byte[] RecvBuffer = new byte[4];


        private static RecvMemData RecvDataInstacne = null;
        RecvMemData()
        {

        }

       

        static object locker = new object();


        public static RecvMemData GetInstance()
        {
            lock (locker)
            {
                if (RecvDataInstacne == null)
                {
                    RecvDataInstacne = new RecvMemData();
                    return RecvDataInstacne;
                }
            }

            return RecvDataInstacne;
        }

        public void SendCommErrorMsg()
        {
            InterfaceMsg interfaceMsg = new InterfaceMsg();
            interfaceMsg.ID = 100;
            interfaceMsg.Name = "通讯故障";
            if(m_delControlBoardMsg!=null)
            {
                Logger.WriteErrorLog("通信故障或设备断电，请退出程序!");
                Logger.WriteRunLog("发送消息:" + interfaceMsg.Name);
                m_delControlBoardMsg(interfaceMsg, "通信故障或设备断电，请退出程序!");
                Logger.WriteRunLog("消息发送完成");
            }
            
        }

        public bool SendMsg()
        {
            int Grp, Index;

            foreach (var item in CEmsAct.GetInstance().ltMsg)
            {
                Grp = item.ID / 8;
                Index = 7 - item.ID % 8;
                var BitRes = (RecvBuffer[Grp] >> Index);
                BitRes = (BitRes & 0x1);
                if (BitRes > 0)
                {
                    if(item.Name.Contains("设备报错"))
                    {
                        var ErrorInfo = CEmsAct.GetInstance().McuCard.QueryErrorInfo(0);
  
                        Logger.WriteErrorLog(ErrorInfo);
                        Logger.WriteRunLog("发送消息:" + item.Name);
                        m_delControlBoardMsg(item, ErrorInfo);
                        Logger.WriteRunLog("发送消息完成");
                        return true;
                    }
                    else
                    {
                        Logger.WriteRunLog("发送消息:" + item.Name);
                        m_delControlBoardMsg(item, "");
                        Logger.WriteRunLog("发送消息完成");
                    }
                   
                }
            }

            return false;

        }

    }
}
